package com.supermap.panes;

import com.supermap.Helper;
import com.supermap.data.Enum;
import com.supermap.data.*;
import com.supermap.panes.advance.SettingPane;
import com.supermap.realspace.Camera;
import com.supermap.realspace.LookAt;
import com.supermap.realspace.TrackingLayer3D;
import com.supermap.realspace.spatialanalyst.AnalysisQuality;
import com.supermap.realspace.spatialanalyst.Viewshed3D;
import com.supermap.ui.Action3D;
import com.supermap.ui.SceneControl;
import com.supermap.ui.Tracking3DListener;

import javax.swing.*;
import java.awt.*;

public class Viewshed3DAnalystSettingPane extends SettingPane {
    private SceneControl sceneControl;

    private JTextField viewerXTxt;
    private JTextField viewerYTxt;
    private JTextField viewerZTxt;
    private JTextField directionTxt;
    private JTextField pitchTxt;
    private JTextField distanceTxt;
    private JTextField horizontalFovTxt;
    private JTextField verticalFovTxt;
    private JButton hintLineColorBtn;
    private JButton visibleAreaColorBtn;
    private JButton hiddenAreaColorBtn;
    private JComboBox<AnalysisQuality> analysisQualityCbx;
    private JTextField inViewportTxt;

    private Viewshed3D viewshed3D;
    private final Tracking3DListener tracking3DListener = tracking3DEvent -> {
        if (sceneControl.getAction() == Action3D.CREATE_LINE) {
            Geometry3D geometry = tracking3DEvent.getGeometry();
            GeoLine3D geoLine3D = (GeoLine3D) geometry;
            Point3Ds point3Ds = geoLine3D.getPart(0);
            Point3D point3D = point3Ds.getItem(0);
            distanceTxt.setText(String.format("%.2f", geoLine3D.getLength()));
            viewerXTxt.setText(String.format("%.2f", point3D.getX()));
            viewerYTxt.setText(String.format("%.2f", point3D.getY()));
            viewerZTxt.setText(String.format("%.2f", point3D.getZ()));
            if (viewshed3D == null) {
                viewshed3D = new Viewshed3D(sceneControl.getScene());
                viewshed3D.setViewerPosition(point3D);
                viewshed3D.setInViewport(0);
                viewshed3D.setDistance(Double.parseDouble(distanceTxt.getText()));
                viewshed3D.setHintLineColor(hintLineColorBtn.getForeground());
                viewshed3D.setVisibleAreaColor(visibleAreaColorBtn.getForeground());
                viewshed3D.setHiddenAreaColor(hiddenAreaColorBtn.getForeground());
                viewshed3D.setHorizontalFov(Double.parseDouble(horizontalFovTxt.getText()));
                viewshed3D.setVerticalFov(Double.parseDouble(verticalFovTxt.getText()));
                viewshed3D.setQuality((AnalysisQuality) analysisQualityCbx.getSelectedItem());
            } else {
                viewshed3D.setDirection(Double.parseDouble(directionTxt.getText()));
                viewshed3D.setPitch(Double.parseDouble(pitchTxt.getText()));
            }
            viewshed3D.setDistDirByPoint(point3Ds.getItem(point3Ds.getCount() - 1));
            viewshed3D.build();
            directionTxt.setText(String.format("%.2f", viewshed3D.getDirection()));
            pitchTxt.setText(String.format("%.2f", viewshed3D.getPitch()));
        }
    };

    public Viewshed3DAnalystSettingPane(String title, SceneControl sceneControl) {
        super(title, new Object[]{sceneControl});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        sceneControl = (SceneControl) args[0];
    }

    @Override
    protected void initializeCore() {
        this.confirmBtn.setVisible(false);
        super.mainPanel.setLayout(new BorderLayout());

        JToolBar toolBar = new JToolBar();
        super.mainPanel.add(toolBar, BorderLayout.NORTH);

        JButton drawRectBtn = new JButton("绘制");
        toolBar.add(drawRectBtn);
        drawRectBtn.addActionListener(e -> {
            sceneControl.setAction(Action3D.CREATE_LINE);
            sceneControl.addTrackingListener(tracking3DListener);
        });

        JButton clearBtn = new JButton("清空");
        toolBar.add(clearBtn);
        clearBtn.addActionListener(e -> clearViewshed3DLayer());

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(3, 1));
        super.mainPanel.add(centerPanel, BorderLayout.CENTER);

        JPanel viewerInfoPanel = new JPanel();
        viewerInfoPanel.setBorder(BorderFactory.createTitledBorder("观察者信息"));
        viewerInfoPanel.setLayout(new GridBagLayout());
        centerPanel.add(viewerInfoPanel);

        viewerInfoPanel.add(new JLabel("X:"), new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        viewerXTxt = new JTextField();
        viewerInfoPanel.add(viewerXTxt, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        viewerInfoPanel.add(new JLabel("Y:"), new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        viewerYTxt = new JTextField();
        viewerInfoPanel.add(viewerYTxt, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        viewerInfoPanel.add(new JLabel("Z:"), new GridBagConstraints(0, 2, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        viewerZTxt = new JTextField();
        viewerInfoPanel.add(viewerZTxt, new GridBagConstraints(1, 2, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        viewerInfoPanel.add(new JLabel("方位角(度):"), new GridBagConstraints(0, 3, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        directionTxt = new JTextField();
        viewerInfoPanel.add(directionTxt, new GridBagConstraints(1, 3, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        viewerInfoPanel.add(new JLabel("俯仰角(度):"), new GridBagConstraints(0, 4, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        pitchTxt = new JTextField();
        viewerInfoPanel.add(pitchTxt, new GridBagConstraints(1, 4, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        viewerInfoPanel.add(new JLabel("可视距离(米):"), new GridBagConstraints(0, 5, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        distanceTxt = new JTextField();
        viewerInfoPanel.add(distanceTxt, new GridBagConstraints(1, 5, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        JPanel parametersPanel = new JPanel();
        parametersPanel.setBorder(BorderFactory.createTitledBorder("参数设置"));
        parametersPanel.setLayout(new GridBagLayout());
        centerPanel.add(parametersPanel);

        parametersPanel.add(new JLabel("水平视角(度):"), new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        horizontalFovTxt = new JTextField();
        parametersPanel.add(horizontalFovTxt, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        parametersPanel.add(new JLabel("垂直视角(度):"), new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        verticalFovTxt = new JTextField();
        parametersPanel.add(verticalFovTxt, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        hintLineColorBtn = new JButton("提示线颜色");
        parametersPanel.add(hintLineColorBtn, new GridBagConstraints(0, 2, 2, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        visibleAreaColorBtn = new JButton("可见区域颜色");
        parametersPanel.add(visibleAreaColorBtn, new GridBagConstraints(0, 3, 2, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        hiddenAreaColorBtn = new JButton("不可见区域颜色");
        parametersPanel.add(hiddenAreaColorBtn, new GridBagConstraints(0, 4, 2, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        parametersPanel.add(new JLabel("分析精度:"), new GridBagConstraints(0, 5, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        analysisQualityCbx = new JComboBox<>();
        parametersPanel.add(analysisQualityCbx, new GridBagConstraints(1, 5, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        parametersPanel.add(new JLabel("视口:"), new GridBagConstraints(0, 6, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));
        inViewportTxt = new JTextField();
        parametersPanel.add(inViewportTxt, new GridBagConstraints(1, 6, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, Helper.DefaultInsets, 0, 0));

        initializeDefaultValues();
    }

    @Override
    protected void doConfirm() {

    }

    @Override
    protected void doClose() {
        clearViewshed3DLayer();
        for (Component component : getComponents()) {
            component.disable();
        }
    }

    private void initializeDefaultValues() {
        inViewportTxt.setEnabled(false);
        inViewportTxt.setText("第一视口");

        Enum[] analysisQualities = AnalysisQuality.getEnums(AnalysisQuality.class);
        for (Enum analysisQuality : analysisQualities) {
            analysisQualityCbx.addItem((AnalysisQuality) analysisQuality);
        }
        analysisQualityCbx.addItemListener(e -> {
            if (viewshed3D != null) {
                viewshed3D.setQuality((AnalysisQuality) e.getItem());
                viewshed3D.build();
            }
        });

        hintLineColorBtn.setForeground(Color.BLUE);
        hintLineColorBtn.setBackground(Color.BLUE);
        hintLineColorBtn.addActionListener(e -> {
            Color hintLineColor = JColorChooser.showDialog(this, "提示线颜色", hintLineColorBtn.getForeground());
            if (hintLineColor != null) {
                hintLineColorBtn.setForeground(hintLineColor);
                hintLineColorBtn.setBackground(hintLineColor);
                if (viewshed3D != null) {
                    viewshed3D.setHintLineColor(hintLineColorBtn.getForeground());
                    viewshed3D.build();
                }
            }
        });

        visibleAreaColorBtn.setForeground(Color.GREEN);
        visibleAreaColorBtn.setBackground(Color.GREEN);
        visibleAreaColorBtn.addActionListener(e -> {
            Color visibleAreaColor = JColorChooser.showDialog(this, "可见区域颜色", visibleAreaColorBtn.getForeground());
            if (visibleAreaColor != null) {
                visibleAreaColorBtn.setForeground(visibleAreaColor);
                visibleAreaColorBtn.setBackground(visibleAreaColor);
                if (viewshed3D != null) {
                    viewshed3D.setVisibleAreaColor(visibleAreaColorBtn.getForeground());
                    viewshed3D.build();
                }
            }
        });

        hiddenAreaColorBtn.setForeground(Color.RED);
        hiddenAreaColorBtn.setBackground(Color.RED);
        hiddenAreaColorBtn.addActionListener(e -> {
            Color hiddenAreaColor = JColorChooser.showDialog(this, "不可见区域颜色", hiddenAreaColorBtn.getForeground());
            if (hiddenAreaColor != null) {
                hiddenAreaColorBtn.setForeground(hiddenAreaColor);
                hiddenAreaColorBtn.setBackground(hiddenAreaColor);
                if (viewshed3D != null) {
                    viewshed3D.setHiddenAreaColor(hiddenAreaColorBtn.getForeground());
                    viewshed3D.build();
                }
            }
        });

        viewerXTxt.setEditable(false);
        viewerYTxt.setEditable(false);
        viewerZTxt.setEditable(false);
        directionTxt.setEditable(false);
        pitchTxt.setEditable(false);
        distanceTxt.setEditable(false);
        horizontalFovTxt.setText("90");
        verticalFovTxt.setText("60");
        analysisQualityCbx.setSelectedItem(AnalysisQuality.HIGH);
    }

    private void clearViewshed3DLayer() {
        if (viewshed3D != null) {
            viewshed3D.clear();
            viewshed3D = null;
        }
        sceneControl.setAction(Action3D.PAN);
        sceneControl.removeTrackingListener(tracking3DListener);
        TrackingLayer3D trackingLayer = sceneControl.getScene().getTrackingLayer();
        int viewshed3DIndex = trackingLayer.indexOf("viewshed3D");
        if (viewshed3DIndex != -1) {
            trackingLayer.remove(viewshed3DIndex);
        }
    }
}
